package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import javafx.scene.control.TreeView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list() {
        return "region/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Region region) {
        regionService.saveOrUpdate(region);
        return "redirect:/region/list";
    }

    @RequestMapping("/queryMenus")
    @ResponseBody
    public Object queryMenus(RegionQueryObject qo) {
        List<Region> list = regionService.queryMenus(qo);
        //[  {id:xx,text: "全部区域",tags: ['4']}, {id:xx,text: xx,tags: [xx]} ]
        /*ArrayList<Map<String,Object>> treeviews = new ArrayList<>();
        for (Region region : list) {
            Map<String, Object> treeview = region.getTreeview();
            treeviews.add(treeview);
        }
        return treeviews;*/
        if (qo.isTreeview()){
            return list.stream().map(Region::getTreeview).collect(Collectors.toList());
        }
        return list;
    }

}
